package sharewithus.content;


import org.apache.log4j.PropertyConfigurator;
import org.dbunit.IDatabaseTester;
import org.dbunit.JdbcDatabaseTester;
import org.dbunit.database.DatabaseConfig;
import org.dbunit.dataset.IDataSet;
import org.dbunit.ext.oracle.Oracle10DataTypeFactory;
import org.dbunit.operation.DatabaseOperation;
import org.dbunit.util.fileloader.DataFileLoader;
import org.dbunit.util.fileloader.FlatXmlDataFileLoader;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;




/**
 * 
 * @author Bruce Wang
 *
 */
public abstract class ContentBaseTestCase extends AbstractDependencyInjectionSpringContextTests {
	protected IDatabaseTester dbTester;
    //protected final Logger log = LoggerFactory.getLogger(getClass());
    protected IDatabaseTester munion;
    protected IDatabaseTester rpt;
	

    public ContentBaseTestCase() {
        super.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
       
        try {
        	munion=new JdbcDatabaseTester("com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/sharewithus","sharewithus","root","123456");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
    }

	protected String[] getConfigLocations() {
		return new String[] { "classpath:/config/spring/applicationContext.xml"

		};
	}
	
	@Override
	public void onSetUp(){
		try {
			if(getDataSetXmlPath()!=null){
				PropertyConfigurator.configure(ContentBaseTestCase.class.getClassLoader().getResource("log4j.properties"));  
				dbTester=getDBConnnection();
				DataFileLoader fileLoader=new FlatXmlDataFileLoader();
				IDataSet dataSet=fileLoader.load(getDataSetXmlPath());
				dbTester.setDataSet(dataSet);
				dbTester.setSetUpOperation(DatabaseOperation.INSERT);
				dbTester.setTearDownOperation(getDBOperation());
				dbTester.getConnection().getConfig().setProperty(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);
				dbTester.getConnection().getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new Oracle10DataTypeFactory());
				dbTester.onSetup();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected IDatabaseTester getDBConnnection() {
		return munion;
	}
	
	protected DatabaseOperation getDBOperation(){
		return DatabaseOperation.DELETE;
	}

	@Override
	public void onTearDown(){
		try {
			if(getDataSetXmlPath()!=null){
				dbTester.onTearDown();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取数据库DataSetXML文件地址
	 * @return
	 */
	protected String getDataSetXmlPath() {
		return null;
	}
}
